<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="NEW 未支付订单,PAY已经支付订单,CANCEL超时取消订单" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择NEW 未支付订单,PAY已经支付订单,CANCEL超时取消订单" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="0表示未删除，1表示已经删除" prop="del">
        <el-select v-model="queryParams.del" placeholder="请选择0表示未删除，1表示已经删除" clearable>
          <el-option
            v-for="dict in dict.type.del"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型 DAILY普通单，PROMOTION促销订单" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型 DAILY普通单，PROMOTION促销订单" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="订单唯一标识" align="center" prop="outTradeNo" />
      <el-table-column label="NEW 未支付订单,PAY已经支付订单,CANCEL超时取消订单" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="订单总金额" align="center" prop="totalAmount" />
      <el-table-column label="订单实际支付价格" align="center" prop="payAmount" />
      <el-table-column label="支付类型，微信-银行-支付宝" align="center" prop="payType" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="头像" align="center" prop="headImg" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="0表示未删除，1表示已经删除" align="center" prop="del">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.del" :value="scope.row.del"/>
        </template>
      </el-table-column>
      <el-table-column label="订单类型 DAILY普通单，PROMOTION促销订单" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="收货地址 json存储" align="center" prop="receiverAddress" />
      <el-table-column label="总重量" align="center" prop="totalWeight" />
      <el-table-column label="运费" align="center" prop="payFee" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单主表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单唯一标识" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入订单唯一标识" />
        </el-form-item>
        <el-form-item label="NEW 未支付订单,PAY已经支付订单,CANCEL超时取消订单" prop="state">
          <el-select v-model="form.state" placeholder="请选择NEW 未支付订单,PAY已经支付订单,CANCEL超时取消订单">
            <el-option
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入订单总金额" />
        </el-form-item>
        <el-form-item label="订单实际支付价格" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入订单实际支付价格" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="headImg">
          <el-input v-model="form.headImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="0表示未删除，1表示已经删除" prop="del">
          <el-select v-model="form.del" placeholder="请选择0表示未删除，1表示已经删除">
            <el-option
              v-for="dict in dict.type.del"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单类型 DAILY普通单，PROMOTION促销订单" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型 DAILY普通单，PROMOTION促销订单">
            <el-option
              v-for="dict in dict.type.order_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货地址 json存储" prop="receiverAddress">
          <el-input v-model="form.receiverAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="总重量" prop="totalWeight">
          <el-input v-model="form.totalWeight" placeholder="请输入总重量" />
        </el-form-item>
        <el-form-item label="运费" prop="payFee">
          <el-input v-model="form.payFee" placeholder="请输入运费" />
        </el-form-item>
        <el-divider content-position="center">订单子表信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddProductOrderItem">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteProductOrderItem">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="productOrderItemList" :row-class-name="rowProductOrderItemIndex" @selection-change="handleProductOrderItemSelectionChange" ref="productOrderItem">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="订单编号" prop="outTradeNo" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.outTradeNo" placeholder="请输入订单编号" />
            </template>
          </el-table-column>
          <el-table-column label="产品id" prop="productId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.productId" placeholder="请输入产品id" />
            </template>
          </el-table-column>
          <el-table-column label="商品名称" prop="productName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.productName" placeholder="请输入商品名称" />
            </template>
          </el-table-column>
          <el-table-column label="购买数量" prop="buyNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.buyNum" placeholder="请输入购买数量" />
            </template>
          </el-table-column>
          <el-table-column label="购物项商品总价格" prop="totalAmount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.totalAmount" placeholder="请输入购物项商品总价格" />
            </template>
          </el-table-column>
          <el-table-column label="购物项商品单价" prop="amount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.amount" placeholder="请输入购物项商品单价" />
            </template>
          </el-table-column>
          <el-table-column label="购物项商品总重量" prop="totalWeight" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.totalWeight" placeholder="请输入购物项商品总重量" />
            </template>
          </el-table-column>
          <el-table-column label="购物项商品重量" prop="weight" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.weight" placeholder="请输入购物项商品重量" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/order/order";

export default {
  name: "Order",
  dicts: ['order_status', 'del', 'order_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProductOrderItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单主表表格数据
      orderList: [],
      // 订单子表表格数据
      productOrderItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        state: null,
        del: null,
        orderType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单主表列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outTradeNo: null,
        state: null,
        createTime: null,
        totalAmount: null,
        payAmount: null,
        payType: null,
        nickname: null,
        headImg: null,
        userId: null,
        del: null,
        updateTime: null,
        orderType: null,
        receiverAddress: null,
        totalWeight: null,
        payFee: null
      };
      this.productOrderItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单主表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.productOrderItemList = response.data.productOrderItemList;
        this.open = true;
        this.title = "修改订单主表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.productOrderItemList = this.productOrderItemList;
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单主表编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 订单子表序号 */
    rowProductOrderItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 订单子表添加按钮操作 */
    handleAddProductOrderItem() {
      let obj = {};
      obj.outTradeNo = "";
      obj.productId = "";
      obj.productName = "";
      obj.productImg = "";
      obj.buyNum = "";
      obj.totalAmount = "";
      obj.amount = "";
      obj.totalWeight = "";
      obj.weight = "";
      this.productOrderItemList.push(obj);
    },
    /** 订单子表删除按钮操作 */
    handleDeleteProductOrderItem() {
      if (this.checkedProductOrderItem.length == 0) {
        this.$modal.msgError("请先选择要删除的订单子表数据");
      } else {
        const productOrderItemList = this.productOrderItemList;
        const checkedProductOrderItem = this.checkedProductOrderItem;
        this.productOrderItemList = productOrderItemList.filter(function(item) {
          return checkedProductOrderItem.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleProductOrderItemSelectionChange(selection) {
      this.checkedProductOrderItem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
